package com.project.server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.server.common.Result;
import com.project.server.dto.OrderRequest;
import com.project.server.model.Order;
import com.project.server.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "订单管理")
@RestController
@RequestMapping("/api/order")
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;

    @Operation(summary = "创建订单")
    @PostMapping
    public Result<Void> create(@RequestBody @Validated OrderRequest request, HttpServletRequest httpRequest) {
        orderService.create(request, httpRequest);
        return Result.success();
    }

    @Operation(summary = "获取我的订单列表")
    @GetMapping("/my")
    public Result<List<Order>> getMyOrders(HttpServletRequest request,Long status) {
        return Result.success(orderService.getMyOrders(request,status));
    }

    @Operation(summary = "获取订单详情")
    @GetMapping("/{id}")
    public Result<Order> getById(@PathVariable Long id) {
        return Result.success(orderService.getById(id));
    }

    @Operation(summary = "取消订单")
    @PutMapping("/cancel/{id}")
    public Result<Void> cancel(@PathVariable Long id, HttpServletRequest request) {
        orderService.cancel(id, request);
        return Result.success();
    }

    @Operation(summary = "确认订单")
    @PutMapping("/confirm/{id}")
    public Result<Void> confirm(@PathVariable Long id) {
        orderService.confirm(id);
        return Result.success();
    }

    @Operation(summary = "完成订单")
    @PutMapping("/complete/{id}")
    public Result<Void> complete(@PathVariable Long id) {
        orderService.complete(id);
        return Result.success();
    }

    @Operation(summary = "分页查询订单列表")
    @GetMapping("/page")
    public Result<Page<Order>> page(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Long restaurantId,
            @RequestParam(required = false) Long userId,
            @RequestParam(required = false) Integer status) {
        return Result.success(orderService.page(page, pageSize, restaurantId, userId, status));
    }






}
